package rexsee.script;

public class BlockCondition extends AbstractBlock {

	private final AbstractBlock condition;
	private final AbstractBlock body;

	public BlockCondition(AbstractBlock parent, AbstractBlock condition, AbstractBlock body) {
		super(parent, "");
		this.condition = condition;
		this.body = body;
	}

	@Override
	public int getChildCount(int index) {
		return 0;
	}

	@Override
	public AbstractBlock getChild(int index) {
		return null;
	}

	@Override
	public Object run() {
		if (condition == null || body == null) return false;
		Object rtn = condition.run();
		boolean fail = rtn instanceof Boolean && ((Boolean) rtn) == false;
		if (fail) {
			return false;
		} else {
			body.run();
			return true;
		}
	}

}
